<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Opens a connection to a Microsoft SQL Server database</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.sqlsrv-connect" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sqlsrv_connect</h1>
  <p class="verinfo">(No version information available, might only be in Git)</p><p class="refpurpose"><span class="refname">sqlsrv_connect</span> &mdash; <span class="dc-title">Opens a connection to a Microsoft SQL Server database</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.sqlsrv-connect-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sqlsrv_connect</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$serverName</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$connectionInfo</code></span>
  ] ) : <span class="type">resource</span></div>

  <p class="para rdfs-comment">
   Opens a connection to a Microsoft SQL Server database. By default, the connection 
   is attempted using Windows Authentication. To connect using SQL Server 
   Authentication, include &quot;UID&quot; and &quot;PWD&quot; in the connection options array.
  </p>
 </div>

 
 <div class="refsect1 parameters" id="refsect1-function.sqlsrv-connect-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">serverName</code></dt>

     <dd>

      <p class="para">
       The name of the server to which a connection is established. To connect 
       to a specific instance, follow the server name with a backward slash 
       and the instance name (e.g. serverName\sqlexpress).
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">connectionInfo</code></dt>

     <dd>

      <p class="para">
       An associative array that specifies options for connecting to the server. 
       If values for the UID and PWD keys are not specified, the connection 
       will be attempted using Windows Authentication. For a complete list of supported 
       keys, see <a href="http://msdn.microsoft.com/en-us/library/ff628167.aspx" class="link external">&raquo;&nbsp;SQLSRV Connection Options</a>.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.sqlsrv-connect-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   A connection resource. If a connection cannot be successfully opened, <strong><code>FALSE</code></strong> is returned.
  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.sqlsrv-connect-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2571">
    <p><strong>Example #1 Connect using Windows Authentication.</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$serverName&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"serverName\\sqlexpress"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//serverName\instanceName<br /><br />//&nbsp;Since&nbsp;UID&nbsp;and&nbsp;PWD&nbsp;are&nbsp;not&nbsp;specified&nbsp;in&nbsp;the&nbsp;$connectionInfo&nbsp;array,<br />//&nbsp;The&nbsp;connection&nbsp;will&nbsp;be&nbsp;attempted&nbsp;using&nbsp;Windows&nbsp;Authentication.<br /></span><span style="color: #0000BB">$connectionInfo&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">"Database"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"dbName"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_connect</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$serverName</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$connectionInfo</span><span style="color: #007700">);<br /><br />if(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Connection&nbsp;established.&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Connection&nbsp;could&nbsp;not&nbsp;be&nbsp;established.&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2572">
    <p><strong>Example #2 Connect by specifying a user name and password.</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$serverName&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"serverName\\sqlexpress"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//serverName\instanceName<br /></span><span style="color: #0000BB">$connectionInfo&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">"Database"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"dbName"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"UID"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"userName"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PWD"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_connect</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$serverName</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$connectionInfo</span><span style="color: #007700">);<br /><br />if(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Connection&nbsp;established.&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Connection&nbsp;could&nbsp;not&nbsp;be&nbsp;established.&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2573">
    <p><strong>Example #3 Connect on a specifed port.</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$serverName&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"serverName\\sqlexpress,&nbsp;1542"</span><span style="color: #007700">;&nbsp;</span><span style="color: #FF8000">//serverName\instanceName,&nbsp;portNumber&nbsp;(default&nbsp;is&nbsp;1433)<br /></span><span style="color: #0000BB">$connectionInfo&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">"Database"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"dbName"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"UID"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"userName"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PWD"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_connect</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$serverName</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$connectionInfo</span><span style="color: #007700">);<br /><br />if(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Connection&nbsp;established.&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />}else{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Connection&nbsp;could&nbsp;not&nbsp;be&nbsp;established.&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.sqlsrv-connect-notes">
  <h3 class="title">注释</h3>
  <p class="para">
   By default, the <span class="function"><strong>sqlsrv_connect()</strong></span> uses connection pooling to 
   improve connection performance. To turn off connection pooling (i.e. force a 
   new connection on each call), set the &quot;ConnectionPooling&quot; option in the 
   $connectionOptions array to 0 (or <strong><code>FALSE</code></strong>). For more information, see 
   <a href="http://msdn.microsoft.com/en-us/library/cc644930.aspx" class="link external">&raquo;&nbsp;SQLSRV Connection Pooling</a>.
  </p>
  <p class="para">
  The SQLSRV extension does not have a dedicated function for changing which 
  database is connected to. The target database is specified in the 
  $connectionOptions array that is passed to sqlsrv_connect. To change the 
  database on an open connection, execute the following query &quot;USE dbName&quot; 
  (e.g. sqlsrv_query($conn, &quot;USE dbName&quot;)).
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.sqlsrv-connect-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="sqlsrv_close.html" class="function" rel="rdfs-seeAlso">sqlsrv_close()</a> - Closes an open connection and releases resourses associated with the connection</span></li>
    <li class="member"><span class="function"><a href="sqlsrv_errors.html" class="function" rel="rdfs-seeAlso">sqlsrv_errors()</a> - Returns error and warning information about the last SQLSRV operation performed</span></li>
    <li class="member"><span class="function"><a href="sqlsrv_query.html" class="function" rel="rdfs-seeAlso">sqlsrv_query()</a> - Prepares and executes a query</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>